package com.senven.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.OrganAffair;
import com.senven.api.service.OrganAffairService;
import com.senven.api.utils.DateTimeConvert;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author LQP
 */
@RequestMapping("/api/organ/affair")
@Controller
public class OrganAffairController {
    @Autowired
    private OrganAffairService organAffairService;

    @GetMapping("/getOrganAffairList")
    @ResponseBody
    public ResponsePage<OrganAffair> queryOrganAffairList(@RequestParam(value = "current",defaultValue = "1")int current,
                                                          @RequestParam(value = "size",defaultValue = "10")int size){
        return organAffairService.queryOrganAffairs(current,size);
    }



    /**
     * 社团ID获取社团事务
     * @param oid
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganAffairListByOid")
    @ResponseBody
    public ResponsePage<OrganAffair> queryOrganAffairList(@RequestParam("oid")int oid,
                                                          @RequestParam(value = "current",defaultValue = "1")int current,
                                                          @RequestParam(value = "size",defaultValue = "10")int size){
        return organAffairService.queryOrganAffairList(oid, current,size);
    }


    /**
     * 社团ID获取社团事务
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganAffairListByUid")
    @ResponseBody
    public ResponsePage<OrganAffair> queryOrganAffairList(@RequestParam("uid")String uid,
                                                          @RequestParam(value = "current",defaultValue = "1")int current,
                                                          @RequestParam(value = "size",defaultValue = "10")int size){
        return organAffairService.queryOrganAffairsByUid(uid, current,size);
    }

    /**
     * 获取社团事务
     * @param oid
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganAffairListByName")
    @ResponseBody
    public ResponsePage<OrganAffair> queryOrganAffairListByName(@RequestParam("oid")int oid,
                                                                @RequestParam("name")String name,
                                                                @RequestParam(value = "current",defaultValue = "1")int current,
                                                                @RequestParam(value = "size", defaultValue = "10")int size){
        return organAffairService.queryOrganAffairList(oid, name, current,size);
    }


    /**
     * 删除社团事务
     * @param id
     * @return
     */
    @GetMapping("/deleteOrganAffairById")
    @ResponseBody
    public ResponseResult deleteOrganAffair(@RequestParam("id")int id){
        try {
            int result = organAffairService.deleteOrganAffair(id);
            if (result < 1) {
                return ReponseResultUtil.error(-1, "事务删除失败");
            } else {
                return ReponseResultUtil.success("事务删除成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    /**
     * 更新社团事务
     * @param organAffair
     * @return
     */
    @PostMapping("/updateOrganAffair")
    @ResponseBody
    public ResponseResult updateOrganAffair(@RequestBody OrganAffair organAffair){
        int result = organAffairService.updateOrganAffair(organAffair);
        if (result < 1){
            return ReponseResultUtil.error(-1,"事务更新失败");
        }else{
            return ReponseResultUtil.success("事务更新成功");
        }
    }


    /**
     * 插入社团事务
     * @param organAffair
     * @return
     */
    @PostMapping("/insertOrganAffair")
    @ResponseBody
    public ResponseResult insertOrganAffair(@RequestBody OrganAffair organAffair){
        //日期转换
        Date date = new Date();
        String datetime = DateTimeConvert.dateToStr(date,DateTimeConvert.STANDARD_FORMAT);
        date = DateTimeConvert.strToDate(datetime);
        organAffair.setCreateTime(date);
        OrganAffair affair = organAffairService.queryOrganAffairsByUidAndName(organAffair.getUid(),organAffair.getLogname(),organAffair.getOid());

        //判断事务是否存在
        if (affair != null && affair.getLogname() != ""){
            return ReponseResultUtil.error(-1,"请勿重复参加");
        }

        int result = organAffairService.insertOrganAffair(organAffair);
        if (result < 1){
            return ReponseResultUtil.error(-1,"事务插入失败");
        }else{
            return ReponseResultUtil.success("事务插入成功");
        }
    }

    /**
     * 导出活动报名
     * @return
     */
    @GetMapping("/exportActivity")
    @ResponseBody
    public List<OrganAffair> exportActivity(){
        return organAffairService.exportActivity();
    }


    /**
     * 导出社团报名
     * @return
     */
    @GetMapping("/exportOrgan")
    @ResponseBody
    public List<OrganAffair> exportOrgan(){
        return organAffairService.exportOrgan();
    }

}
